package com.forlove.app.qselect.login.widget;

import android.content.Context;
import android.support.annotation.AttrRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.forlove.app.R;

import static android.view.animation.AnimationUtils.loadAnimation;


/**
 * 启动时的授权页面
 * Created by yjl on 2017/10/2.
 */

public class RegisterPhoneView extends RelativeLayout {
    public static final int POSITION_INPUT_PHONE = 0;//输入手机号码
    public static final int POSITION_CODE_IDENTIFY = 1;//验证码验证界面
    public static final int POSITION_SET_PW = 2;//设置密码界面
    private View mRootView;
    private Context mContext;
    private LinearLayout inputPhoneView;//手机验证页面
    private LinearLayout codeIdentifyView;//验证码验证页面
    private LinearLayout setPwView; //设置密码页面
    private TextView bt_register_nextstep;
    private TextView bt_identify_nextstep;
    private TextView bt_register_finish;
    private int currentPosition = POSITION_INPUT_PHONE;

    public RegisterPhoneView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public RegisterPhoneView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RegisterPhoneView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mRootView = inflater.inflate(R.layout.layout_registor_phone_view, this);
        inputPhoneView = (LinearLayout) mRootView.findViewById(R.id.view_input_phone);
        codeIdentifyView = (LinearLayout) mRootView.findViewById(R.id.view_input_code);
        setPwView = (LinearLayout) mRootView.findViewById(R.id.view_input_pw);
        bt_register_nextstep = (TextView) inputPhoneView.findViewById(R.id.bt_register_nextstep);
        bt_identify_nextstep = (TextView) codeIdentifyView.findViewById(R.id.bt_identify_nextstep);
        bt_register_finish = (TextView) setPwView.findViewById(R.id.bt_register_finish);
    }

    public int getCurrentPosition() {
        return currentPosition;
    }

    public void showInputPhoneView() {
        codeIdentifyView.setVisibility(GONE);
        setPwView.setVisibility(GONE);
        final Animation animationOut = AnimationUtils.loadAnimation(mContext, R.anim.push_right_out);
        codeIdentifyView.setAnimation(animationOut);

        if (!inputPhoneView.isShown()) {
            inputPhoneView.setVisibility(VISIBLE);
            Animation animationIn = loadAnimation(mContext, R.anim.push_left_in);
            inputPhoneView.setAnimation(animationIn);
            animationIn.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    animationOut.start();
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            animationIn.start();
        }
        currentPosition = POSITION_INPUT_PHONE;
    }

    public void showCodeIdentifyView() {

        inputPhoneView.setVisibility(GONE);
        setPwView.setVisibility(GONE);
        if (!codeIdentifyView.isShown()) {
            codeIdentifyView.setVisibility(VISIBLE);
            Animation animationIn = AnimationUtils.loadAnimation(mContext, R.anim.push_right_in);
            if (currentPosition == POSITION_SET_PW) {
                animationIn = AnimationUtils.loadAnimation(mContext, R.anim.push_left_in);
                final Animation animationOut = AnimationUtils.loadAnimation(mContext, R.anim.push_right_out);
                setPwView.setAnimation(animationOut);
                animationIn.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        animationOut.start();
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            } else if (currentPosition == POSITION_INPUT_PHONE) {
                final Animation animationOut = loadAnimation(mContext, R.anim.push_left_out);
                inputPhoneView.setAnimation(animationOut);
                animationIn.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        animationOut.start();
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
            }
            codeIdentifyView.setAnimation(animationIn);
            animationIn.start();
        }

        currentPosition = POSITION_CODE_IDENTIFY;

    }


    public void showSetPwView() {
        inputPhoneView.setVisibility(GONE);
        codeIdentifyView.setVisibility(GONE);
        if (!setPwView.isShown()) {
            setPwView.setVisibility(VISIBLE);
            Animation animationIn = loadAnimation(mContext, R.anim.push_right_in);
            setPwView.setAnimation(animationIn);
            final Animation animationOut = loadAnimation(mContext, R.anim.push_left_out);
            codeIdentifyView.setAnimation(animationOut);

            animationIn.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    animationOut.start();
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            animationIn.start();
        }
        currentPosition = POSITION_SET_PW;
    }


}
